package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.InspectionTask;
import com.neusoft.neuiotms.dcom.model.QInspectionTask;
import com.neusoft.neuiotms.dcom.model.enums.InspectionTaskStatus;
import com.neusoft.neuiotms.dcom.repository.CustomizedInspectionTaskRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedInspectionTaskRepositoryImpl extends AbstractRepository
        implements CustomizedInspectionTaskRepository {

    @Override
    public List<InspectionTask> index(final Searchable searchable) {
        final QInspectionTask qInspectionTask = QInspectionTask.inspectionTask;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InspectionTask> query = this.query().selectFrom(qInspectionTask);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<InspectionTask> page(final Searchable searchable, final Pageable pageable) {
        final QInspectionTask qInspectionTask = QInspectionTask.inspectionTask;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InspectionTask> query = this.query().selectFrom(qInspectionTask);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QInspectionTask qInspectionTask = QInspectionTask.inspectionTask;

        if (!StringUtils.isEmpty(searchable.getStrValue("name"))) {
            where.and(super.containsStr(qInspectionTask.name, searchable, "name"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("id"))) {
            where.and(super.equalsLong(qInspectionTask.id, searchable, "id"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("inspectionRuleId"))) {
            where.and(super.equalsLong(qInspectionTask.inspectionRule.id, searchable, "inspectionRuleId"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("inspectionRuleCode"))) {
            where.and(super.equalsStr(qInspectionTask.inspectionRule.code, searchable, "inspectionRuleCode"));
        }
        // 部门codes集合
        final String[] orgCodes = searchable.getStrArray("orgCodes");
        if ((orgCodes != null) && (orgCodes.length > 0)) {
            where.and(super.inStrings(qInspectionTask.inspectionRule.orgCode, searchable, "orgCodes"));
        }

        // 职能Id集合
        final String[] functionIds = searchable.getStrArray("functionIds");
        if ((functionIds != null) && (functionIds.length > 0)) {
            where.and(super.inStrings(qInspectionTask.inspectionRule.inspectionType, searchable, "functionIds"));
        }

        //任务开始时间
        if (!StringUtils.isEmpty(searchable.getStrValue("startTaskTimeBegin"))) {
            where.and(
                    super.afterOrEqualsDate(qInspectionTask.startTaskTime, searchable, "startTaskTimeBegin"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("startTaskTimeEnd"))) {
            where.and(super.beforeOrEqualsDate(qInspectionTask.startTaskTime, searchable, "startTaskTimeEnd"));
        }
        //任务结束时间
        if (!StringUtils.isEmpty(searchable.getStrValue("endTaskTimeBegin"))) {
            where.and(super.afterOrEqualsDate(qInspectionTask.endTaskTime, searchable,
                    "endTaskTimeBegin"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("endTaskTimeEnd"))) {
            where.and(super.beforeOrEqualsDate(qInspectionTask.endTaskTime, searchable,
                    "endTaskTimeEnd"));
        }
        //巡检类型
        if (!StringUtils.isEmpty(searchable.getStrValue("inspectionType"))) {
            where.and(super.equalsStr(qInspectionTask.inspectionRule.inspectionType, searchable, "inspectionType"));
        }
        //巡检状态
        if (!StringUtils.isEmpty(searchable.getStrValue("status"))) {
            where.and((super.equalsEnum(qInspectionTask.status, searchable, "status",
                    value -> InspectionTaskStatus.valueOf(value))));
        }

        final String[] multiStatus = searchable.getStrArray("multiStatus");
        if ((multiStatus != null) && (multiStatus.length > 0)) {
            final BooleanBuilder whereGlobal = new BooleanBuilder();
            for (int i = 0; i < multiStatus.length; i++) {
                searchable.put("status" + i, multiStatus[i]);
                whereGlobal.or((super.equalsEnum(qInspectionTask.status, searchable, "status" + i,
                        value -> InspectionTaskStatus.valueOf(value))));
            }
            where.and(whereGlobal);
        }

        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return InspectionTask.class;
    }

}
